package lab1;

import java.io.*;
import java.util.zip.DataFormatException;


public class Main {
	
	public static void main(String[] args) throws IOException {
		BufferedReader in= null;
	    PrintWriter out=null;
		try{
			if (args.length==0) {		
            in=new BufferedReader(new FileReader("in.txt"));
            out=new PrintWriter(new FileWriter("out.txt"));	
			}else{
				in=new BufferedReader(new FileReader(args[0]+".txt"));
				out=new PrintWriter(new FileWriter(args[1]+".txt"));	
			}
			MyArrayList ArrList=new MyArrayList();	
			String input = in.readLine();
			int temp;
			if (input==null){
				out.println("File is empty");
			}else{
				try{
				while (input != null) {
					String[] str = input.split(" ");
					out.println("Command: "+input);	    		
					if (str[0].equals("add")){
						ArrList.add(Integer.parseInt(str[1]));
					}else if (str[0].equals("get")){
						temp=ArrList.get(Integer.parseInt(str[1]));
						out.println("	Result: "+temp);
					}else if (str[0].equals("isEmpty")){
						out.println("	Result: "+ArrList.isEmpty());
					}else if (str[0].equals("remove")){
						ArrList.remove(Integer.parseInt(str[1]));
					}else if (str[0].equals("removeEl")){
						ArrList.removeEl(Integer.parseInt(str[1]));
					}else if (str[0].equals("clear")){
						ArrList.clear();
					}else{
						throw new DataFormatException("Unknown command '"+str[0]+"'");
					}
					out.print("	ArrayList: " + ArrList.toString() + " ");
					out.println("size: " + ArrList.size());
					input = in.readLine();
				}
				}catch(IOException|DataFormatException|IndexOutOfBoundsException|NullPointerException e){
					out.println(e.toString());
				}
				
			}
		}finally{
			if (out!=null) out.close();
			if (in!=null) in.close();			
		}
	    
    } 
}
